/**
** 用户自定受众 整体 插件
**author: ericever
**create time: 2015-7-24
**/
;(function($, window, document, undefined){
    'use strict';
    var old = $.fn.Anchor;
    var defaults = {
        list:[],
        data:[],
        fun:null
    };
    var Anchor = function (element, options) {
        var self = this;
        this.$element = $(element);
        this.config = $.extend(true,{}, defaults, options);
        var list = this.config.list;
        var data = this.config.data;
        var tags = [];
        if(data.customAudiences) $.map(data.customAudiences,function(item){ tags.push(item.id) });
        function format(arr,tag,b,fun){
            var tag = tag || "name";
            if(arr && arr.length){
                if(!b) arr.unshift({id:"",text:""});
                for(var i=0;i<arr.length;i++){
                    var the = arr[i];
                    the.text = the[tag]
                }
            }
            return arr;
        }
        function build(){
            var the = $(element);
            if(the.children().length>0) return;
            var drop = $('<div class="input-drop dropdown ca-choose">');
            var combox = $('<div class="combox-wrapper">');
            var drop2 = $('<div class="input-drop dropdown">');//包含不包含
            var selwrapper = $('<div class="select-wrapper">');
            var mulsel = $('<select class="form-control audience-select2" style="width:100%" aria-hidden="true" multiple>');
            combox.append(drop2).append(selwrapper.append(mulsel));
            //重新组织 dataIndex,修复因为解绑或者添加账号引起的bug
            var _customAudiences = [],_value=-1;
            //console.log(data);
            $.each(list,function(i,fbAccountObj){
                if(fbAccountObj.fbAccountId == data.fbAccountId){
                    _value = i;
                    _customAudiences = fbAccountObj.customAudiences;
                    return false;
                }else{
                    //说明此账号已经被解绑了
                    _value = -1;

                }
            });
            drop.empty().bootDrop({
                list:list,
                holder: I18N.getText('createAd', 'optionPlsAccount'),
                prop:"fbAccountName",
                value:_value
            }).off("selectChanged").on("selectChanged",function(e,o){
                mulsel.empty().siblings("span+select2").remove();
                mulsel.select2({
                    data:format(o.data.customAudiences,null,1),
                }).unbind("select2:select").on("select2:select",function(e){
                    $(element).trigger("addTag",e.params.data);//-------添加了tag
                }).unbind("select2:unselect").on("select2:unselect",function(e){
                    $(element).trigger("removeTag",e.params.data);//------删除了tag
                }).select2("val",tags);
                $(element).trigger("accountChange",o);
            });
            if(data.hasOwnProperty("dataIndex")){
                //去掉了賦值
                if(_value!= -1 && _customAudiences.length != 0){
                    mulsel.select2({data:format(_customAudiences,null,1)}).select2("val",tags);
                }
            }
            drop2.empty().bootDrop({
                list:[{value:"0",text:I18N.getText('createAd', 'exclude')},{value:"1",text:I18N.getText('createAd', 'include')}],
                value:data.hasOwnProperty("isClude")?(data.isClude?1:0):1,
                prop:"text"
            }).on("selectChanged",function(e,o){
                $(element).trigger("cludeChange",o)});
                mulsel.select2();
                setTimeout(function(){
                    the.append(drop).append(combox);
                });
        };

        function destory(){
            $(element).empty();
        };

        this.startup = function(){
            build();
        }
    };

    /***
    ** 入口
    **/
    $.fn.hybrbox = function(options){
        var bd = new Anchor(this,options);
        bd.startup();
        return this;
    }
}(window.j$ || jQuery, window, document));